<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>人工发送短信管理</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link href="../../layui/css/iconfont/iconfont.css" rel="stylesheet" type="text/css">
    <link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <link href="../../css/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" type="text/css">
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.8.3.js"></script>
    <script src="../../js/util.js"></script>
    <script src="../../js/jquery-ui-1.10.4.custom.min.js"></script>
    <script type="text/javascript">
    
    function artificialSend(){
    location.href = "smsArtificialRecordImport.html";
    }
    
        layui.use(['form', 'table', 'jquery', 'element', 'laydate', 'layer'], function () {
            var form = layui.form;
            var $ = layui.$;
            var laydate = layui.laydate;
            var layer = layui.layer;
            var table = layui.table;
            var batchNumber;//批次号
            laydate.render({
                elem: '#sendTime'
                , type: 'date'
            });
            var tableDate = {
                elem: "#table",//表格id
                id: "modelReload", //id
                url: '/artificialRecord/find',
                method: 'post',
                request: {
                    pageName: 'page',
                    limitName: 'limit'
                },
                response: {
                    statusName: 'code',
                    statusCode: 0,
                    msgName: 'msg',
                    countName: 'count',
                    dataName: 'data'
                },
                page: true,
                count: "count",
                limit: 10,
                limits: [10, 20, 30, 40, 50],
                layout: ['prev', 'page', 'next'],
                prev: "上一页",
                next: "下一页",
                cols: [[ //表头
                    {field: 'province', title: '地市\区县'}
                    , {field: 'name', title: '项目名称'}
                    , {field: 'sendTime', title: '手工发送时间'}
                    , {field: 'batchNumber', title: '批次号'}
                    , {field: 'operation', title: '操作', toolbar: '#barDemo'}
                ]], done: function (res, curr, count) {
                }
            };

            table.on('tool(table)', function (obj) {
                var data = obj.data;
                if(data.state==-1){
                    layer.msg("该批次还未导入");
                }
                else  if (obj.event == 'detail') {
                 location.href='smsArtificialRecordDetailManage.html?batchNumber='+data.batchNumber;
                }
        });

            table.render(tableDate);

/*     		var source = [];
    		//自动补全下拉
    		$("#mgrName").keyup(function() {
    			var key = $("#mgrName").val();
    			var cityId=$("#cityId").val();
    			var countyId=$("#countyId").val();
    			var url = '/util/selectMgrNameLike';
    			var param = {
    			    cityId : cityId,
    			    countyId : countyId,
    				key : key
    			};
    			$.ajax({
    				type : 'post',
    				async : false,
    				url : url,
    				data : param,
    				success : function(json) {
    					data = json.data;
    					for (var i = 0; i < data.length; i++) {
    						source[i] = data[i].name;
    					}
    				},
    				error : function() {
    					layer.msg('系统异常，请联系管理员处理!');
    				}
    			}
    			);
    		});
    
    		$("#mgrName").autocomplete({
    			source : source,
    			messages : { //message设置不出现匹配关键字的结果
    				noResults : '',
    				results : function() {}
    			}
    		}); */
    		
    		$("#mgrName").autocomplete({
       source : function( request, response ) {
        var key = $("#mgrName").val();
        var cityId=$("#cityId").val();
        var countyId=$("#countyId").val();
        var url = '/util/selectMgrNameLike';
        var param = {
            cityId : cityId,
            countyId : countyId,
         key : key
        };
                 $.ajax({
         type : 'post',
         async : false,
         url : url,
         data : param,
         success : function(json) {
          var data = json.data;
          source = [];
          for (var i = 0; i < data.length; i++) {
           source[i] = data[i].name;
          }
          console.log(source);
          response( source );
         },
         error : function() {
          layer.msg('系统异常，请联系管理员处理!');
         }
        }
        );
            },
       messages : { //message设置不出现匹配关键字的结果
        noResults : '',
        results : function() {
        }
       }
      });


        $('#query').on('click', function () {
           var mgrName = $("#mgrName").val().trim();
            var cityId = $("#cityId").val();
            var mgrId = $("#mgrId").val();
            if (
                mgrId == "" && user.duty == 2
            ) {
                layer.msg("您必须选择一个项目");
            } else {
                var index = layer.load(1, {
                    shade: [0.1,'#fff'] //0.1透明度的白色背景
                });
                table.reload('modelReload', {
                    method: 'post',
                    page: {
                        curr: 1
                    },
                    where: {
                        mgrId: mgrId,
                        cityId: cityId,
                        mgrName: mgrName,
                        countyId:$("#countyId").val(),
                        sendTime: $("#sendTime").val(),
                        batchNumber: $("#batchNumber").val()
                    }
                });
                layer.close(index);
            }

        });

        //身份识别
        $(document).ready(function () {
            $.ajax({
                type: 'post',
                async: false,
                url: '/util/findUser',
                success: function (json) {
                    user = json;
                    if (json.duty == 1) {//地市
                        $("#cityId").val(json.cityId);
                        $("#cityId").attr("disabled", true);
                        var data3 = selectMgrInfo(json.cityId);
                        appendMgr(data3);
                        var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                        form.render();
                    } else if (json.duty == 2) {//区县
                        $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            var data3 = selectMgrInfo(json.countyId);
                            appendMgr(data3);
                            var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            form.render();
                       // appendMgr(json.list);                       
                    }else if (json.duty == 3) {//项目
                            appendMgr(json.list);
                            var data3 = selectMgrInfo(json.countyId);
                            appendMgr(data3);
                            var data2 = selectCountyId(json.cityId);
                            appendCounty(data2);
                            $("#cityId").val(json.cityId);
                            $("#cityId").attr("disabled", true);
                            $("#countyId").val(json.countyId);
                            $("#countyId").attr("disabled", true);
                            form.render();
                        }
                }, error: function () {
                    layer.msg('系统异常，请联系管理员处理!');
                }
            });
        });

         //监听区县
            form.on('select(cityId)', function (data) {
                var data2 = selectCountyId(data.value);
                appendCounty(data2);
                var data1 = selectMgrInfo(data.value);
                 appendMgr(data1);
            });
            //根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接区县
            function appendCounty(data){
                if (data.length != null) {
                    var options = '<option value="">请先选择地市</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].countyId + '">' + data[i].countyName + '</option>';
                    }
                    $('#countyId').html(options);
                } else {
                    $('#countyId').html('该地市下没有区县');
                }
                form.render();
            }
            
            //监听项目
            form.on('select(countyId)', function (data) {
            if(data.value == ""){appendMgr(select(cityId))}
            else{
                var data3 = selectMgrInfo(data.value);
                appendMgr(data3);}
               /*  var data1 = selectMgrInfo(data.value);
                appendMgr(data1); */
            });
                        //根据城市ID查询项目
            function selectMgrInfo(cityId) {
                var data;
                var url = '/util/findByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
                      
            //根据城市ID和区县查询项目
            function selectMgrInfo(countyId) {
                var data;
                var url = '/util/findByMgrId?countyId=' + countyId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            //动态拼接项目
            function appendMgr(data) {
                if (data.length != null) {
                    var options = '<option value="">请选择</option>';
                    for (var i = 0; i < data.length; i++) {
                        options += '<option value="' + data[i].id + '">' + data[i].name + '</option>';
                    }
                    $('#mgrId').html(options);
                } else {
                    $('#mgrId').html('<option value="">该区县下没有项目</option>');
                }
                form.render();
            }

        //监听项目编号，并监听批次号
        form.on('select(mgrId)', function (data) {
            var url = '/artificialRecord/findBatchNumber?mgrId=' + data.value;
            $.ajax({
                    type: 'post',
                    async: false,
                    url: url,
                    success: function (json) {
                        data = json.data;
                        if (data.length != null) {
                            var options = '<option value="">请选择</option>';
                            for (var i = 0; i < data.length; i++) {
                                options += '<option value="' + data[i].batchNumber + '">' + data[i].batchNumber + '</option>';
                            }
                            $('#batchNumber').html(options);
                        } else {
                            $('#batchNumber').html('<option value="">该项目下没有记录</option>');
                        }
                        form.render();
                    }, error: function () {
                        layer.msg('系统异常，请联系管理员处理!');
                    }
                }
            );
        });
        });

    </script>
    <script type="text/html" id="barDemo">
        <a href="#" lay-event="detail">查看详情</a>
    </script>
</head>
<body>
<div class="layui-content-body">
    <div class="scroll-box">
        <div class="breadcrumb-box">
              <span class="layui-breadcrumb" lay-separator="|">
  <a href="javascript:returnIndex2()">首页&nbsp;</a>
  <a href=""> &nbsp;短信发送管理 </a>
  <a href=""> &nbsp;人工发送记录查询&nbsp; </a>
</span>
        </div>
        <div class="content-box">
            <div class="form-select-box">
                <div class="layui-form layui-form-pane" action="">
                    <div class="layui-form-item">                       
                        <div class="layui-inline">
                            <label class="layui-form-label">地市</label>
                            <div class="layui-input-block">
                                <select name="cityId" lay-filter="cityId" id="cityId">
                                    <option value="">全部</option>
                                    <option value="11">苏州</option>
                                    <option value="12">淮安</option>
                                    <option value="13">宿迁</option>
                                    <option value="14">南京</option>
                                    <option value="15">连云港</option>
                                    <option value="16">徐州</option>
                                    <option value="17">常州</option>
                                    <option value="18">镇江</option>
                                    <option value="19">无锡</option>
                                    <option value="20">南通</option>
                                    <option value="21">泰州</option>
                                    <option value="22">盐城</option>
                                    <option value="23">扬州</option>
                                    <option value="25">省级</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
								<label class="layui-form-label">区县</label>
								<div class="layui-input-block" style="width:150px">
									<select name="countyId" lay-filter="countyId" id="countyId">
										<option value="">全部</option>
									</select>
								</div>
							</div>
							<div class="layui-inline">
                            <label class="layui-form-label">项目简称</label>
                            <div class="layui-input-block" style="width:150px">
                                <input type="text"  name="mgrName" id="mgrName" lay-verify="required" placeholder="请输入"
                                            autocomplete="off" class="layui-input" />
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">项目名称</label>
                            <div class="layui-input-block">
                                <select name="mgrId" lay-filter="mgrId" id="mgrId">
                                </select>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-inline" style="width:auto;">
                                <label class="layui-form-label">发送日期</label>
                                <div class="layui-input-block">
                                    <input type="text" placeholder="请输入日期"
                                           class="layui-input" id="sendTime">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <label class="layui-form-label">批次号</label>
                                <div class="layui-input-block">
                                    <select name="batchNumber" lay-filter="batchNumber" id="batchNumber">
                                    </select>
                                </div>
                            </div>
                            <div class="layui-inline" pane="">
                                <button class="layui-btn layui-btn-normal" id="query">查询</button>
                                <button class="layui-btn layui-btn-normal" type="button"
									onclick="artificialSend()">人工发送</button>
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="table-box">
                <table class="layui-table" id="table" lay-filter="table">
                </table>
            </div>
        </div>
    </div>
</div>
</body>
</html>